package com.design_mode;

import sun.org.mozilla.javascript.internal.Context;

/**
 * Created by dustaple on 8/3/14.
 */
public class StatePattern {
    public static void main(String[] args) {
        Context_ context_ = new Context_();

        context_.setCurrentState(Context_.STATE1);

        context_.handleState1();
        context_.handleState2();
        context_.handleState3();
    }
}

abstract class AbstractState {
    protected Context_ context_;

    public abstract void handleState1();

    public abstract void handleState2();

    public abstract void handleState3();

    public void setContext_(Context_ context_) {
        this.context_ = context_;
    }
}

class State1 extends AbstractState {
    State1() {
    }

    @Override
    public void handleState1() {
        System.out.println("This is state1111 handling!!!");
    }

    @Override
    public void handleState2() {
        context_.setCurrentState(Context_.STATE2);
        context_.getCurrentState().handleState2();
    }

    @Override
    public void handleState3() {
        context_.setCurrentState(Context_.STATE3);
        context_.getCurrentState().handleState3();
    }
}

class State2 extends AbstractState {
    State2() {
    }

    @Override
    public void handleState1() {
        context_.setCurrentState(Context_.STATE1);
        context_.getCurrentState().handleState1();
    }

    @Override
    public void handleState2() {
        System.out.println("This is state2222 handling!!!");
    }

    @Override
    public void handleState3() {
        context_.setCurrentState(Context_.STATE3);
        context_.getCurrentState().handleState3();
    }
}

class State3 extends AbstractState {
    State3() {
    }

    @Override
    public void handleState1() {
        context_.setCurrentState(Context_.STATE1);
        context_.getCurrentState().handleState1();
    }

    @Override
    public void handleState2() {
        context_.setCurrentState(Context_.STATE2);
        context_.getCurrentState().handleState2();
    }

    @Override
    public void handleState3() {
        System.out.println("This is state3333 handling!!!");
    }
}

class Context_ {
    public final static AbstractState STATE1 = new State1();
    public final static AbstractState STATE2 = new State2();
    public final static AbstractState STATE3 = new State3();

    AbstractState currentState;

    public AbstractState getCurrentState() {
        return currentState;
    }

    public void setCurrentState(AbstractState currentState) {
        this.currentState = currentState;
        this.currentState.setContext_(this);
    }

    public void handleState1() {
        currentState.handleState1();
    }

    public void handleState2() {
        currentState.handleState2();
    }

    public void handleState3() {
        currentState.handleState3();
    }
}
